package com.haust.su.ghyy.hospital.controller.api;

import com.haust.su.ghyy.common.exception.YyghException;
import com.haust.su.ghyy.common.helper.HttpRequestHelper;
import com.haust.su.ghyy.common.result.Result;
import com.haust.su.ghyy.common.result.ResultCodeEnum;
import com.haust.su.ghyy.common.utils.MD5;
import com.haust.su.ghyy.hospital.service.DepartmentService;
import com.haust.su.ghyy.hospital.service.HospitalSetService;
import com.haust.su.ghyy.model.hosp.Department;
import com.haust.su.ghyy.vo.hosp.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author haustsusu
 * @create 2023-04-27-10:48
 */
@RestController
@RequestMapping("/api/hosp")
public class DepartmentController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> Parammap = HttpRequestHelper.switchMap(requestMap);
        //医院编号和科室编号
        String hoscode = (String)Parammap.get("hoscode");
        String depcode = (String)Parammap.get("depcode");

        departmentService.remove(hoscode,depcode);
        return Result.ok();

    }


    //查询医院科室信息
    @PostMapping("department/list")
    public Result finddepartment(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> Parammap = HttpRequestHelper.switchMap(requestMap);


        //当前页与每页数量
        int page= StringUtils.isEmpty(Parammap.get("page")) ?1:Integer.parseInt((String) Parammap.get("page"));
        int limit= StringUtils.isEmpty(Parammap.get("limit")) ?1:Integer.parseInt((String) Parammap.get("limit"));

        //进行签名的校验
        //获取医院编号
        String hoscode =(String)Parammap.get("hoscode");
        //获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign=(String)Parammap.get("sign");

        //根据传递过来的医院编号,查询数据库,查询签名
        String signKey=hospitalSetService.getSignKey(hoscode);

        //吧数据库查询的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        //加入不一致
        if(!hospSign.equals(signKeyMd5)){
            //加入签名不一致,爆出签名不一致异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageResult=departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageResult);



    }


    //上传科室接口saveDepartment
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> Parammap = HttpRequestHelper.switchMap(requestMap);

        /**
         * @Description
         * @author susu haust rg1902
         * @date 2023/4/27 10:53
         * 判断医院签名是否正确
         */
        //获取医院编号
        String hoscode =(String)Parammap.get("hoscode");
        //获取医院系统传递过来的签名,签名进行MD5加密
        String hospSign=(String)Parammap.get("sign");

        //根据传递过来的医院编号,查询数据库,查询签名
        String signKey=hospitalSetService.getSignKey(hoscode);

        //吧数据库查询的签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否一致
        //加入不一致
        if(!hospSign.equals(signKeyMd5)){
            //加入签名不一致,爆出签名不一致异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }


        //签名成功后调用service方法
        System.out.println(Parammap);
        departmentService.save(Parammap);
        return Result.ok();
    }



}
